﻿using AutoMapper;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.Error;
using B.S.BasicData.Infrastructure.InProfaces;
using B.S.BasicData.Write.Api.Application.Command.ProductsCommand;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHander.ProductsCommandHander
{
    public class ProductsUpdCommandHander : IRequestHandler<ProductsUpdCommand, ApiResult<Products>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Products> ProductsRepository;

        public ProductsUpdCommandHander(IMapper mapper, IBaseRepository<Products> productsRepository)
        {
            this.mapper = mapper;
            ProductsRepository = productsRepository;
        }

        public Task<ApiResult<Products>> Handle(ProductsUpdCommand request, CancellationToken cancellationToken)
        {
            ApiResult<Products> result = new ApiResult<Products>();

            var list = ProductsRepository.GetAll().FirstOrDefault(x => x.ProductsId == request.ProductsId);


            list.ProductsId = request.ProductsId;
            list.ShopId = request.ShopId;
            list.ProductionBatch = request.ProductionBatch;
            list.ShelfLife = request.ShelfLife;
            list.LicenseNumber = request.LicenseNumber;
            list.Manufacturers = request.Manufacturers;
            list.CoverImage = request.CoverImage;
            list.CreateName = "吕岳其";
            list.CreateTime = DateTime.Now;
            list.UpdateName = "魏金磊";
            list.UpdateTime = DateTime.Now;
            list.IsDel = false;

            var upd = ProductsRepository.Update(list).Result;

            if (upd > 0)
            {
                result.Code = ApiEnum.success;
                result.Msg = "修改成功";
            }
            else
            {
                result.Code = ApiEnum.error;
                result.Msg = "修改失败";
            }

            return Task.FromResult(result);

        }
    }
}
